﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Diagnostics;
using Hardcodet.Wpf.TaskbarNotification;

namespace dmm
{
    /// <summary>
    /// Interaction logic for RecordControl.xaml
    /// </summary>
    public partial class RecordControl : Window
    {
        int idx = 1;
        bool recording = true;
        int record_seconds = 0;
        int last_tick = System.Environment.TickCount;

        private Recorder recorder;

        public RecordControl(Recorder rec)
        {
            recorder = rec;
            
            InitializeComponent();
           
        }

        private void onAnimationCompleted(object sender, EventArgs e)
        {
            if (recording)
            {
                show_image();
              

                animation.BeginAnimation(Image.WidthProperty, animation);

               
            }
        }

        private void show_image()
        {
            //string filename = "pack://siteoforigin:,,,/img/button" + idx + ".png";
            //string filename = @"G:\work\demomaker\img\button" + idx + ".png";
            string filename = "icons/tray"+idx+".ico";
            BitmapImage img = new BitmapImage();
            img.BeginInit();
            img.UriSource = new Uri(filename, UriKind.RelativeOrAbsolute);
            img.EndInit();
            ctrl.Source = img;

            MyNotifyIcon.IconSource = img;

            idx++;
            if (idx > 2) idx = 1;

            //compute time

            int tick = System.Environment.TickCount;
            int interv = ( tick - last_tick)/1000;

            if (interv > 0)
            {
                last_tick = tick;
                record_seconds += interv;

                //Debug.Write("elapsed:" + record_seconds + "\n");

                int tmp = record_seconds;
                int hour = tmp / 3600;
                tmp -= hour * 3600;
                int min = tmp / 60;
                tmp -= min * 60;
                int sec = tmp;

                string str_time = String.Format("{0:D2}:{1:D2}:{2:D2}",hour,min,sec);
                //str_tim
                record_time.Text = str_time;               
                
            }

        }

        private void onButtonClick(object sender, EventArgs e)
        {
            recording = !recording;

            if (recording)
            {
                if (recorder.full_screen)
                    WindowState = WindowState.Minimized;

                last_tick = System.Environment.TickCount;
                animation.BeginAnimation(Image.WidthProperty, animation);
                recorder.start();
            }
            else
            {
                if (recorder.full_screen)
                    WindowState = WindowState.Normal;

                recorder.stop();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            recorder.start();

            //TODO::this does not work yet for unknown reason
            string title = "Sothink canvas to video";
            string text = "Click to stop recording";
            MyNotifyIcon.ShowBalloonTip(title, text, MyNotifyIcon.Icon);
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            //clean up notifyicon (would otherwise stay open until application finishes)
            MyNotifyIcon.Dispose();

            base.OnClosing(e);
        }

       

        private void MyNotifyIcon_TrayLeftMouseDown(object sender, RoutedEventArgs e)
        {
            onButtonClick(sender, e);
          
        }


    }   
}
